﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ManageYWLB : System.Web.UI.Page
{
    //Datacon dataconn = new Datacon();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["ID"] == null)
            {
                Response.Write("<script>alert('非法登录！');location='../Default.aspx'</script>");
            }
            else
            {
                //dataconn.bindinfostring(gvQueInfo, "select * from JudgeProblem where c_id in(select c_id from Course where teacher_id='" + Session["ID"] + "')order by ID DESC", "ID");
                using (DataClassesDataContext ds = new DataClassesDataContext())
                {
                    gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                           where (q.parentID == -1 || q.parentID == null)
                                           orderby q.id descending
                                           select q;
                    gvQueInfo.DataBind();

                }
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            if (TextBox1.Text == "")
            {
                gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                       where (q.parentID == -1 || q.parentID == null)
                                       orderby q.id descending
                                       select q;
                gvQueInfo.DataBind();
            }
            else
            {
                gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                       where (q.parentID == -1 || q.parentID == null)
                                       where q.mingcheng.Contains(TextBox1.Text.Trim())
                                       orderby q.id descending
                                       select q;
                gvQueInfo.DataBind();

            }
        }
    }
    protected void gvQueInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Response.Redirect("ChangeYWLB.aspx");
    }
    protected void gvQueInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            var r1 = (from q in ds.YeWuZiLiao
                      where q.id == Convert.ToInt32(gvQueInfo.DataKeys[e.RowIndex].Value)
                      select q).FirstOrDefault();
            var rb = (from q in ds.YeWuZiLiao
                      where q.parentID == r1.id
                      select q).Any();
            if (rb == false)
            {
                ds.YeWuZiLiao.DeleteOnSubmit(r1);
                ds.SubmitChanges();
            }
            else
            {
                Response.Write("<script>alert('该业务类别已有对应的业务资料，不能删除！')</script>");
            }
        }
        Response.Redirect("ManageYWLB.aspx");
    }
    protected void gvQueInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        using (DataClassesDataContext ds = new DataClassesDataContext())
        {
            gvQueInfo.PageIndex = e.NewPageIndex;
            if (TextBox1.Text == "")
            {
                gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                       orderby q.id descending
                                       select q;
                gvQueInfo.DataBind();
            }
            else
            {
                gvQueInfo.DataSource = from q in ds.YeWuZiLiao
                                       where q.mingcheng.Contains(TextBox1.Text.Trim())
                                       orderby q.id descending
                                       select q;
                gvQueInfo.DataBind();

            }
        }
    }
}